package com.example.partner.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.partner.databinding.ItemContainerReceivedMessageBinding;
import com.example.partner.databinding.ItemContainerSentMessageBinding;
import com.example.partner.model.domain.Im;

import java.util.List;

//ChatAdapter是用于管理RecyclerView中的数据的适配器，将数据与特定的视图持有者(ViewHolder)进行绑定。
public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private final List<Im> ims;//消息列表
    private final Long senderId;
    private static String toimg;
    private static Context context;

    //    VIEW_TYPE_SENT和VIEW_TYPE_RECEIVED用来区分发送消息的视图和接收消息的视图。
    public static final int VIEW_TYPE_SENT=1;
    public  static final int VIEW_TYPE_RECEIVED=2;

    public ChatAdapter(List<Im> ims, String toimg,Long senderId, Context context) {//构造函数
        this.context=context;
        this.ims = ims;
        this.toimg=toimg;
        this.senderId = senderId;
    }

    @NonNull// 确保返回的字符串不为空
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(viewType==VIEW_TYPE_SENT){//发送消息的视图
            return new SentMessageViewHolder(
                    ItemContainerSentMessageBinding.inflate(
                            //使用item_container_sent_message布局文件来为RecyclerView的父视图
                            // 创建一个新的视图，并返回一个与该布局绑定的`ItemContainerSentMessageBinding`对象。
                            LayoutInflater.from(parent.getContext()),parent,false//false表示总是从布局文件中创建新的视图。
                    )
            );
        }else{
            return new ReceivedMessageViewHolder(
                    ItemContainerReceivedMessageBinding.inflate(
                            LayoutInflater.from(parent.getContext()),parent,false
                    )
            );
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if(getItemViewType(position)==VIEW_TYPE_SENT){//发送消息的视图
            ((SentMessageViewHolder) holder).setData(ims.get(position));
        }else {
            ((ReceivedMessageViewHolder) holder).setData(ims.get(position));
        }
    }

    @Override
    public int getItemCount() {
        return ims.size();
    }

    @Override
    public int getItemViewType(int position) {
        if(ims.get(position).getUid().equals(senderId)){
            return VIEW_TYPE_SENT;
        }else {
            return VIEW_TYPE_RECEIVED;
        }
    }

    static class SentMessageViewHolder extends RecyclerView.ViewHolder{
        private final ItemContainerSentMessageBinding binding;


        SentMessageViewHolder(ItemContainerSentMessageBinding itemContainerSentMessageBinding) {
            super(itemContainerSentMessageBinding.getRoot());
            binding=itemContainerSentMessageBinding;
        }
        void setData(Im im){
            binding.textMessage.setText(im.getText());
            binding.textDateTime.setText(im.getCreateTime());
        }
    }

    static class ReceivedMessageViewHolder extends RecyclerView.ViewHolder{
        private final ItemContainerReceivedMessageBinding binding;


        ReceivedMessageViewHolder(ItemContainerReceivedMessageBinding itemContainerReceivedMessageBinding) {
            super(itemContainerReceivedMessageBinding.getRoot());
            binding=itemContainerReceivedMessageBinding;
        }
        void setData(Im im){
            binding.textMessage.setText(im.getText());
            binding.textDateTime.setText(im.getCreateTime());
            ImageView avatar=binding.imageProfile;
            Glide.with(context).load(toimg).into(avatar);
        }
    }
}
